﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;

namespace Controller
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new View.Menu());
        }

    }

    class Orders
    {
        private string customerphone;
        private string[] order;
        private string[] comments;
        private int index;
        private DataTable orders;

        public Orders(string customerphone)
        {
            this.customerphone = customerphone;
            this.order = new string[100];
            this.comments = new string[100];
            this.index = 0;
            orders = new DataTable();
            orders.Columns.Add("Dish Name");
            orders.Columns.Add("Cost");
        }

        public string addOrder(string dishname,string comment)
        {
            string msg = Model.DataAccessLayer.checkAvailability(dishname);
            if (msg == "המנה זמינה ונוספה בהצלחה להזמנה")
            {
                order[index] = dishname;
                comments[index] = comment;
                index++;
                object[] row = new object[2];
                row[0] = dishname;
                row[1] = Model.DataAccessLayer.getCost(dishname);
                orders.Rows.Add(row);
            }
            return msg;
        }

        public string removeOrder(string dishname)
        {
            for (int i = 0; i <= index; i++)
            {
                if (order[i] == dishname)
                {
                    order[i] = "";
                    comments[i] = "";
                    for (int j = 0; j < orders.Rows.Count; j++)
                    {
                        if (orders.Rows[j][0].ToString() == dishname)
                        {
                            orders.Rows[j].Delete();
                        }
                    }
                    return "המנה הוסרה בהצלחה";
                }
            }
            return "המנה לא קיימת בהזמנה";
        }

        public string createInvoice()
        {
            double totalcost = 0;
            string date = DateTime.Now.ToString("dd/MM/yyyy");
            string time = DateTime.Now.ToString("HH:mm:ss tt");
            string msg = Model.DataAccessLayer.getCustomerDetails(customerphone) + "\n\n"
                         + "order time: " + date + "   " + time + "\n\n";

            bool vip = Model.DataAccessLayer.isVip(customerphone);

            //add to sales table
            for (int i = 0; i < index; i++)
            {
                if (order[i] != "")
                {
                    double cost = Model.DataAccessLayer.getCost(order[i]);
                    if (vip) { cost = cost * 0.9; }
                    totalcost += cost;

                    Model.DataAccessLayer.addSaledDish(order[i], customerphone, Convert.ToString(cost), date, time);

                    //send to cook and less ingridients from the kitchen
                    Model.DataAccessLayer.sendToCook(order[i], customerphone,comments[i]);

                    msg += order[i] + ",   " + cost + ",  "+comments[i]+"\n";
                }
            }

            msg += "\ntotal cost: " + totalcost;
            if (vip)
            {
                msg += "\nbecause you VIP you saved: " + totalcost / 9;
            }

            return msg;
        }

        public DataTable getOrders()
        {
            return orders;
        }

        public string[] getDishes()
        {
            int j = 0;
            string[] res = new string[index];
            for (int i = 0; i < index; i++)
            {
                if (order[i] != "")
                {
                    res[j] = order[i];
                    j++;
                }
            }
            return res;
        }
    }

    static class Statics
    {
        public static DataTable getVIPCustomers()
        {
            return Model.DataAccessLayer.getVIPCustomers();
        }

        public static DataTable getLackIngridients()
        {
            return Model.DataAccessLayer.getLackIngridients();
        }

        public static DataTable getRevenue()
        {
            DataTable dt = Model.DataAccessLayer.getSales();

            string[] dishes = new string[100];
            int[] quantities = new int[100];
            double[] cost = new double[100];
            double[] totalcost = new double[100];
            int index = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string dish = dt.Rows[i][0].ToString();
                int j;
                //check if exist
                for (j = 0; j < index; j++)
                {
                    if (dishes[j] == dish)
                    {
                        quantities[j]++;
                        totalcost[j] += Convert.ToDouble(dt.Rows[i][1].ToString());
                        break;
                    }
                }
                //not exist
                if (j == index)
                {
                    dishes[index] = dish;
                    quantities[index] = 1;
                    cost[index] = Model.DataAccessLayer.getCost(dish);
                    totalcost[index] += Convert.ToDouble(dt.Rows[i][1].ToString());
                    index++;
                }
            }

            //sort the arrays
            for (int i = 0; i < index; i++)
            {
                int max = i;
                for (int j = i; j < index; j++)
                {
                    if (totalcost[j] > totalcost[max]) { max = j; }
                }
                //replace max
                string temp1 = dishes[i];
                dishes[i] = dishes[max];
                dishes[max] = temp1;

                int temp2 = quantities[i];
                quantities[i] = quantities[max];
                quantities[max] = temp2;

                double temp3 = cost[i];
                cost[i] = cost[max];
                cost[max] = temp3;

                temp3 = totalcost[i];
                totalcost[i] = totalcost[max];
                totalcost[max] = temp3;
            }

            double revenue = 0;
            dt = new DataTable();
            dt.Columns.Add("dishname");
            dt.Columns.Add("regular cost");
            dt.Columns.Add("quantity");
            dt.Columns.Add("totalcost");
            for (int j = 0; j < index; j++)
            {
                object[] row = new object[4];
                row[0] = dishes[j];
                row[1] = cost[j];
                row[2] = quantities[j];
                row[3] = totalcost[j];
                dt.Rows.Add(row);

                revenue += totalcost[j];
            }
            object[] rev = new object[2];
            dt.Rows.Add(rev);//seperate line
            dt.Rows.Add(rev);//seperate line
            rev[0] = "Total Revenue:";
            rev[1] = revenue;
            dt.Rows.Add(rev);

            return dt;
        }
    }

    static class Menu
    {
        public static string[] getMenu()
        {
            DataTable dt = Model.DataAccessLayer.Menu();
            string[] res = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                res[i] = dt.Rows[i][0].ToString();
            }
            return res;
        }

        public static string[] getIngridients(string dishname)
        {
            DataTable dt = Model.DataAccessLayer.getIngridients(dishname);
            string[] res = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                res[i] = dt.Rows[i][0].ToString();
            }
            return res;
        }

        public static string getCost(string dishname)
        {
            double res = Model.DataAccessLayer.getCost(dishname);
            return Convert.ToString(res);
        }

        public static string addIngridient(string dishname, string ingridient, string quantity)
        {
            return Model.DataAccessLayer.addIngridient(dishname, ingridient, quantity);
        }

        public static string removeIngridient(string dishname, string ingridient)
        {
            return Model.DataAccessLayer.removeIngridient(dishname,ingridient);
        }

        public static string updateCost(string dishname,string cost)
        {
            return Model.DataAccessLayer.updateCost(dishname, cost);
        }

        public static string removeDish(string dishname)
        {
            return Model.DataAccessLayer.removeDish(dishname);
        }

        public static string addDish(string dishname, string cost)
        {
            return Model.DataAccessLayer.addDish(dishname,cost);
        }
    }

    class Customer
    {
        private string phone;

        public Customer(string phone)
        {
            this.phone = phone;
        }

        public bool exist()
        {
            return Model.DataAccessLayer.customerExist(phone);
        }

        public string getMyName()
        {
            return Model.DataAccessLayer.getCustomerName(phone);
        }

        public string getMyPhone()
        {
            return phone;
        }

        public void addMe(string name, string address, bool vip)
        {
            Model.DataAccessLayer.addNewCustomer(phone, name, address, vip);
        }
    }
}
